﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RR;

public partial class orderspec : System.Web.UI.Page
{
    protected RegistRar regrar = new RegistRar();

    private int docID //код документа
    {
        get { return Convert.ToInt32(Request.QueryString["id"]);}
    }       

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetDocInfo();
        }
    }

    protected void GetDocInfo()
    {
        ORDERDOC doc = ORDERDOC.FindByKey(regrar, docID);
        txtCODE.Value = doc.CODE;
        txtDATA.Value = Convert.ToDateTime(doc.DOCUMDATE).ToShortDateString();
        txtOPERATOR.Value = doc.WORKER1.FIO;
        txtWORKER.Value = doc.WORKER2.FIO;
        txtNOTE.Value = doc.NOTE;
        txtARCOBJECT.Value = doc.ARCOBJECT1.NAME;
        switch (doc.OPERTYPE)
        {
            case 0:
                txtOPERTYPE.Value = "передача документа в архив";
                break;
            case 1:
                txtOPERTYPE.Value = "выдача документа из архива";
                break;
            case 2:
                txtOPERTYPE.Value = "изъятие документа из архива";
                break;
        }
        switch (doc.STATE)
        {
            case 0:
                txtSTATE.Value = "документ не проведен";
                break;
            case 1:
                txtSTATE.Value = "документ проведен";
                break;
        }

    }
    protected void grd_OrderSpec_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        var opertype = ORDERDOC.FindByKey(regrar, docID).OPERTYPE;
        var i = ORDERDOC.FindByKey(regrar, docID).WORKER;
        if (opertype != null & i != null)
        {
            int state = opertype.Value;
            int worker = i.Value;

            switch (state)
            {
                case 0:
                    ds_arkLookUp.Where = "it.STATE = 1";
                    e.Editor.DataBind();
                    break;
                case 1:
                    ds_arkLookUp.Where = "it.STATE = 2 and it.WORKER ==" + worker;
                    ds_arkLookUp.DataBind();
                    break;
                case 2:
                    ds_arkLookUp.Where = "it.STATE = 1";
                    e.Editor.DataBind();
                    break;
            }

        }
    }

    protected void grd_OrderSpec_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
    {
        int arc = Convert.ToInt32(e.GetValue("ARCPLACE"));
        if (e.DataColumn.Name == "ARCOBJECT")
        {
            var query = (regrar.ARCPLACE.Where(a => a.CODE == arc)).FirstOrDefault();
            if (query != null)
                e.Cell.Text = String.Format("{0}", query.ARCRACK1.ARCROOM1.ARCOBJECT1.NAME);
        }
        if (e.DataColumn.Name == "ARCROOM")
        {
            var query = (regrar.ARCPLACE.Where(a => a.CODE == arc)).FirstOrDefault();
            if (query != null)
                e.Cell.Text = String.Format("{0}", query.ARCRACK1.ARCROOM1.NOTE);
        }
    }
    protected void grd_OrderSpec_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {
        int state = GetDocStasus();
        switch (state)
        {
            case 1:
                throw new Exception("Документ проведен. Редактировать запрещено!");
                break;
            case -1:
                throw new Exception("Ошибка определения статуса документа!");
                break;
        }
    }
    protected void grd_OrderSpec_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        int state = GetDocStasus();
        switch (state)
        {
            case 1:
                throw new Exception("Документ проведен. Редактировать запрещено!");
                break;
            case -1:
                throw new Exception("Ошибка определения статуса документа!");
                break;
        }
    }

    protected void grd_OrderSpec_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
    {
        ds_arkLookUp.Where = "";
        ds_arkLookUp.DataBind();
    }

    protected void grd_OrderSpec_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        try
        {
            if (e.NewValues["CODE"] == null)
                e.NewValues["CODE"] = regrar.Gen_id("ORDERSPEC_gen");
            if (e.NewValues["ORDERDOC"] == null)
                e.NewValues["ORDERDOC"] = docID;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.InnerException.Message);
        }
    }

    protected void grd_OrderSpec_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
    {
        int state = GetDocStasus();
        switch (state)
        {
            case 1:
                throw new Exception("Документ проведен. Редактировать запрещено!");
                break;
            case -1:
                throw new Exception("Ошибка определения статуса документа!");
                break;
        }
    }

    protected int GetDocStasus()
    {
        var s = ORDERDOC.FindByKey(regrar, docID).STATE;
        if (s != null)
        {
            return s.Value;
        }
        else
        {
            return -1;
        }
    }
}